package com.csit.action;

import java.util.Date;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.ParentPackage;

import com.csit.entity.Buy;
import com.csit.entity.User;
import com.csit.service.BuyDetailService;
import com.csit.service.BuyService;
import com.csit.util.DateUtils;
import com.csit.util.ListAndJsonUtil;
import com.csit.util.ResponseMsgUtil;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 
 * @Description: 采购单
 * @Copyright: 福州骏华信息有限公司 (c)2012
 * @Created Date : 2012-10-31
 * @author longweier
 * @vesion 1.0
 */
@ParentPackage("basePackage")
public class BuyAction extends BaseAction implements ModelDriven<Buy> {
	
	private static final long serialVersionUID = -7559022802192412710L;
	
	private String productids;
	
	private String qtys;
	
	private String prices;
	
	private String notes;
	
	private String deleteids;

	private Date beginDate;
	
	private Date endDate;
	
	@Resource
	private BuyService buyService;
	
	@Resource
	private BuyDetailService buyDetailService;
	
	private Buy buy = new Buy();
	
	public Buy getModel() {
		return buy;
	}
	
	/**
	 * 
	 * @Description: 
	 * @param
	 * @Create: 2012-10-31 下午10:27:45
	 * @author longweier
	 * @update logs
	 * @return
	 * @throws Exception
	 */
	public void add(){
		ResponseMsgUtil msgUtil = new ResponseMsgUtil(false);
		try {
			buy.setShop(getShop());
			msgUtil = buyService.addBuy(buy, productids, qtys, prices, notes);
		} catch (Exception e) {
			e.printStackTrace();
			msgUtil.setMsg("新增后台出错");
		}
		ajaxJson(msgUtil.toJSON());
	}
	
	/**
	 * 
	 * @Description: 删除采购单
	 * @param
	 * @Create: 2012-10-31 下午10:28:02
	 * @author longweier
	 * @update logs
	 * @return
	 * @throws Exception
	 */
	public void delete(){
		ResponseMsgUtil msgUtil = new ResponseMsgUtil(false);
		try {
			msgUtil = buyService.deleteBuy(buy);
		} catch (Exception e) {
			e.printStackTrace();
			msgUtil.setMsg("删除后台出错");
		}
		ajaxJson(msgUtil.toJSON());
	}
	
	/**
	 * 
	 * @Description: 修改
	 * @param
	 * @Create: 2012-10-31 下午10:28:33
	 * @author longweier
	 * @update logs
	 * @return
	 * @throws Exception
	 */
	public void update(){
		ResponseMsgUtil msgUtil = new ResponseMsgUtil(false);
		try {
			msgUtil = buyService.updateBuy(buy, ids, productids, qtys, prices, notes, deleteids);
		} catch (Exception e) {
			e.printStackTrace();
			msgUtil.setMsg("修改后台出错");
		}
		ajaxJson(msgUtil.toJSON());
	}
	
	/**
	 * 
	 * @Description: 
	 * @param
	 * @Create: 2012-10-31 下午10:29:44
	 * @author longweier
	 * @update logs
	 * @return
	 * @throws Exception
	 */
	public void select(){
		buy.setShop(getShop());
		String jsonString = buyDetailService.queryBuyDetail(page, rows, buy, beginDate, endDate);
		ajaxJson(jsonString);
	}

	/**
	 * 
	 * @Description: 获得采购单信息
	 * @param
	 * @Create: 2012-10-31 下午10:35:48
	 * @author longweier
	 * @update logs
	 * @return
	 * @throws Exception
	 */
	public void get(){
		String jsonString = buyService.getBuy(buy);
		ajaxJson(jsonString);
	}
	
	/**
	 * 
	 * 
	 * @Description: 查询明细
	 * @param
	 * @Create: 2012-10-31 下午10:51:39
	 * @author longweier
	 * @update logs
	 * @return
	 * @throws Exception
	 */
	public void selectDetail(){
		if(buy.getBuyID()==0){
			ajaxJson(ListAndJsonUtil.EMPTYJSON);
			return;
		}
		String jsonString = buyService.queryBuyDetail(buy);
		ajaxJson(jsonString);
	}
	/**
	 * 
	 * @Description: 采购单初始化
	 * @param
	 * @Create: 2012-10-31 下午10:51:56
	 * @author longweier
	 * @update logs
	 * @return
	 * @throws Exception
	 */
	public void init(){
		ResponseMsgUtil msgUtil = new ResponseMsgUtil(true);
		String buyCode = buyService.getCode(getShop());
		String buyDate = DateUtils.getDateString(new Date());
		String buyer = (String) getSession(User.LOGINNAME);
		
		msgUtil.setProperty("buyCode", buyCode);
		msgUtil.setProperty("buyDate", buyDate);
		msgUtil.setProperty("buyer", buyer);
		
		ajaxJson(msgUtil.toJSON());
	}
	
	/**
	 * 
	 * @Description: 汇总查询
	 * @param
	 * @Create: 2012-11-1 下午08:52:20
	 * @author longweier
	 * @update logs
	 * @return
	 * @throws Exception
	 */
	public void query(){
		String jsonString = buyDetailService.queryBuyDetail(page, rows, buy,productids,beginDate, endDate);
		ajaxJson(jsonString);
	}
	
	public String getProductids() {
		return productids;
	}

	public void setProductids(String productids) {
		this.productids = productids;
	}
	
	public String getQtys() {
		return qtys;
	}

	public void setQtys(String qtys) {
		this.qtys = qtys;
	}

	public String getPrices() {
		return prices;
	}

	public void setPrices(String prices) {
		this.prices = prices;
	}

	public String getNotes() {
		return notes;
	}

	public void setNotes(String notes) {
		this.notes = notes;
	}

	public String getDeleteids() {
		return deleteids;
	}

	public void setDeleteids(String deleteids) {
		this.deleteids = deleteids;
	}

	public Date getBeginDate() {
		return beginDate;
	}

	public void setBeginDate(Date beginDate) {
		this.beginDate = beginDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}
}
